//
//  KZVocabularyImproveTestReviewView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/10/26.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZVocabularyImproveTestReviewView.h"

// view
#import "DicMainView.h"
#import "KZLetterCardView.h"                // 单词卡片

// tool
#import "BKAudioPlayer.h"                   // 音视频播放

@implementation KZVocabularyImproveTestReviewView
{
    dispatch_block_t        _playAudioTask;
    
    KZLetterCardView        *_letterCardView;           // 单词卡片
    UIButton                *_coursewareButton;         // 课件按钮：显示课件解析
}

- (void)dealloc {
    
    NSLog(@"销毁了");
}

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _letterCardView = [KZLetterCardView new];
    [_letterCardView addTarget:self action:@selector(playAudio) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_letterCardView];
    [_letterCardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(W(10));
        make.right.mas_equalTo(W(-10));
        make.height.equalTo(_letterCardView.mas_width).multipliedBy(200.0/327);
    }];
    
    UIView *coursewareView = [UIView new];
    [self addSubview:coursewareView];
    [coursewareView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(W(-40));
        make.width.height.mas_equalTo(W(100));
        make.centerX.equalTo(self);
    }];
    coursewareView.layer.cornerRadius = W(50);
    coursewareView.layer.masksToBounds = true;
    // 渐变图层
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.bounds = CGRectMake(0, 0, W(100), W(100));
    gradientLayer.position = CGPointMake(W(50), W(50));
    gradientLayer.colors = @[
                             (__bridge id)kzRGBA(254, 205, 98, 1).CGColor,
                             (__bridge id)kzRGBA(255, 149, 0, 1).CGColor,
                             ];
    [coursewareView.layer insertSublayer:gradientLayer atIndex:0];
    
    UIView *shadowView = [UIView new];
    shadowView.backgroundColor = UIColor.whiteColor;
    shadowView.layer.cornerRadius = W(50);
    [self insertSubview:shadowView belowSubview:coursewareView];
    [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(coursewareView);
    }];
    shadowView.layer.shadowOffset = CGSizeMake(0, 0);
    shadowView.layer.shadowRadius = 15;
    shadowView.layer.shadowOpacity = 0.6;
    shadowView.layer.shadowColor = kzRGBA(254, 205, 98, 1).CGColor;
    
    _coursewareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_coursewareButton setImage:[UIImage imageNamed:@"kz-解析课件2"] forState:UIControlStateNormal];
    [_coursewareButton addTarget:self action:@selector(analyze:) forControlEvents:UIControlEventTouchUpInside];
    [coursewareView addSubview:_coursewareButton];
    [_coursewareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(coursewareView);
    }];
}


#pragma mark - setter
- (void)setModel:(KZVocabularyCurrentPackageDetailModel *)model {
    
    _model = model;
    
    [_letterCardView flip:_model.Vocabulary];
}



#pragma mark - 事件
// 播放音频
- (void)playAudio {
    
    // 正在播放中，不播放
    if ([BKAudioPlayer sharedMPlayer].player.status == MAudioStatePlaying) {
        [[BKAudioPlayer sharedMPlayer] playerPaused];
        [[BKAudioPlayer sharedMPlayer] closePlayer];
    }
    
    NSString *url = _model.VocabularyAudioPath;
    [[BKAudioPlayer sharedMPlayer] playerUrl:[NSURL URLWithString:url] seekTotime:0 rateValue:1];
    [[BKAudioPlayer sharedMPlayer] playerPlay];
}
// 课件解析
- (void)analyze:(UIButton *)button {
    
    DicMainView *main = [[DicMainView alloc] initWithTestType:DictTestType_Word isShowRecord:YES];
    main.dictUrl = [NSString stringWithFormat:@"%@api/ZXYYTS/W_ZXYYTS_GetCoursewareByCode",KGetUserDefaults(@"SystemIP")];
    main.dictParam = @{@"stageNo":KGetUserDefaults(@"TestLayer"),
                       @"schoolId":DR_UserInfo.SchoolID,
                       @"stuId":DR_UserInfo.UserID,
                       @"knowledge": _model.VocabularyID,
                       @"levelCode": KGetUserDefaults(@"TestLayer")
                       };
    [main popAnimated:YES];
}

// 开起一个延时任务
- (dispatch_block_t)beginBlockTask:(void(^)(void))block after:(NSTimeInterval)after {
    
    dispatch_block_t task = dispatch_block_create(DISPATCH_BLOCK_DETACHED, block);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(after * NSEC_PER_SEC)), dispatch_get_main_queue(), task);
    return task;
}
@end
